//<script type='text/javascript'>

/**
 * 	csv upload extension (1 file at a time)
 *	communication with the media.php in request folder
 * 	communication with a json string in the moddump for setting up different types of csv upload
 */
 
var Csv = function ( vars ) {
	
	// build csv upload grid
	Object.extend(obj = new Element( "div", {id:"csv", class:"ext csv" }), dashboard.ext.csv);
	obj.insert( obj.grid = new Unit( "Csv upload." ));
		
	return obj.init();
}


dashboard.ext.csv	= {
	
	init		: function() {
		
		this.grid.insertli( this.dropper = new Dropper({ title: "Drop your CSV here", descr: "one at a time, please", type: "media.csv", file_ext: "csv", succes: this.onsucces.bind( this ) }));
			
		options = { onSuccess: this.define.bind( this ), parameters: { set: Object.toJSON({ transfer_type: "js", trg: "csv", type: "csv", action: "seed" }) }};
		new Ajax.Request( 'control.call.php', options );
	
		return this;
	},
	
	define		: function( resp ) {
		
		if( resp.responseJSON );
			this.listtypes = resp.responseJSON;
	},
	
	selected	: 0,
	
	// button to trigger the dashboard
	addbutton	: function() {
		obj = new Element("div", { id:"dashboard.ext.csvbutton", class:"ext_button csv", name:"ext:csv" }).observe('click', dashboard.showhide );
		obj.ext = Csv;
		return obj;
	},

	onsucces	: function( xhr ) {
		
		if( xhr.responseText ) {
		
			this.dropper.addClassName( "done" );
			uploaded = xhr.responseText.evalJSON();
			target = this;
			
			
			this.listtypes.each( function( list ){ uploaded.each( function( upl ){
				target.grid.insertli( new Button({ transfer_type: "js", action: 'process', type: 'csv', descr: list.title, class: 'wide', id: upl.id, objtype: upl.type, listtype: list.type }, target.listen.bind( target ), target.listenProgress.bind( target ), target.showResults.bind( target ))) //
			})})
		
		} else this.onfail();
		
	},
	
	onfail			: function() {
		
		this.update( "Something went wrong. Please reload." );
	},
	
	listen			: function( e ) {
		
		this.grid.insertli( "process started, please wait." );

	},
	
	listenProgress	: function( e ) {
		
		this.grid.insertli( "still processing..." );
	},
	
	showResults		: function( xhr ) {
		
		this.insert( this.result = new Unit( "Results" ));
		
		
		xhr.responseJSON.each( function( l ){ this.insertli( l ); }.bind( this.result ));
		this.result.enableScroll( 340 );
	}
	/*
	
	// seed the dashboard (from filter_seed in csv.php)
	seed	: function( response ) {
	
		// insert file upload
		response.each( function( e ) {
			this.down(0).insert( e );
		}, this );
		this.grid.enableScroll( 200 );
		
		// check every second for a file
		var instance = this;
		var interval = window.setInterval( function(){ instance.csv( interval )}, 1000 );
		
	},
	
	// check for csv
	csv 	: function( e ) {
	
		// check for uploaded files
		if( this.down(1).next(3).down('ul').childElements().first()) {
			
			// clear interval and set type 
			window.clearInterval( e );
			xhttp.send('control.call.php', { trg:"csv", type: "csv", action:"define", vars:{ descr: 'csv upload.' }});
			return this;	
		} 
	
	},
	
	// define type
	definer	: function( descr, content ) {
	
		// insert file upload
		content.each( function( e ) {
			this.down(0).insert( e );
		}, this );
		this.grid.enableScroll( 200 );
	
	},
	
	process	: function( response ) {
		
		
		if( answer = response.responseJSON ) {
			
			this.insert( this.info = new Info( "loading" ));

			// update h1
			this.info.down( 'h1' ).update( 'updated: ' + answer.type + ' (' + answer.count + ' rows)');
			
			// print columns
			
		}
	
	}*/
}
